import { deepClone } from '@/libs/tools'

/**
 * 获取父级控件绑定的form
 * @param {Object} widgetsFlat 扁平化的控件列表
 * @param {Array} widgetHierarchy 控件的层级链
 * @returns ''
 */
 const getWidgetParentBindForm = ({ widgetsFlat, widgetHierarchy = [] }) => {
    let form = ''
    widgetHierarchy = deepClone(widgetHierarchy)
    // 父容器是否绑定对象 form
    if (widgetHierarchy.length > 0) {
        // 向上遍历当前控件的父容器是否绑定数据源
        const findHierarchy = widgetHierarchy.reverse().find((item, index) => {
            let status = false
            const customFeature = widgetsFlat[item]?.widgetData?.eles?.customFeature || {}
            if ((index != 0) && (!!customFeature.form || !!customFeature[':form'])) {
                status = true
            }
            return status
        })
        if (!!findHierarchy) {
            const customFeature = widgetsFlat[findHierarchy].widgetData.eles.customFeature || {}
            form = customFeature[':form'] || customFeature.form
        }
    }
    return form
}

export default getWidgetParentBindForm